package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Brand;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IBrandService;
import com.woniu.supermarket.util.CodeCreateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
@Api(value = "品牌属性的操作")
public class BrandController {


    @Autowired
    IBrandService brandService;

    @ApiOperation(value = "分页查询品牌")
    @GetMapping("BrandList")
    public ResultResponse<PageInfo<Brand>> BrandList(
            @ApiParam(value = "当前页") @RequestParam(value = "now") Integer now,
            @ApiParam("页面大小")@RequestParam(value = "size")Integer size,
            @ApiParam("品牌名") @RequestParam(value = "cons",defaultValue = "") String cons){

        ResultResponse<PageInfo<Brand>> result = null;
        try {
            PageInfo<Brand> pageInfo = brandService.queryBrandByPage(now, size, cons);
            if(pageInfo != null){
                result = new ResultResponse<>(200,"查询品牌成功",pageInfo);
            }else{
                result = new ResultResponse<>(201,"查询品牌失败");
            }
        }catch (RuntimeException e){
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }



    @PostMapping("addBrand")
    @ApiOperation(value = "增加品牌")
    public ResultResponse<Brand> addBrand(
            @ApiParam(value = "接受添加的品牌的信息") @RequestBody Brand brand){

        ResultResponse<Brand> result = null;
        try {
            int i = brandService.addBrand(brand);
            if(i > 0){
                result = new ResultResponse<>(200,"添加品牌成功",brand);
            }else{
                result = new ResultResponse<>(201,"添加品牌失败");
            }
        }catch (RuntimeException e){
            result = new ResultResponse<>(555,"系统错误");

        }
        return result;
    }

    @ApiOperation(value = "修改品牌")
    @PutMapping("modifyBrand")
    public ResultResponse<Brand> modifyBrand(
            @ApiParam(value = "接受修改的品牌的信息") @RequestBody Brand brand){

        ResultResponse<Brand> result = null;
        try {
            int i = brandService.modifyBrand(brand);
            if(i > 0){
                result = new ResultResponse<>(200,"修改品牌成功",brand);
            }else{
                result = new ResultResponse<>(201,"修改品牌失败");
            }
        }catch (RuntimeException e){
            result = new ResultResponse<>(555,"系统错误");

        }
        return result;
    }

    @ApiOperation(value = "删除品牌")
    @DeleteMapping("removeBrand/{id}")
    public ResultResponse<Void> removeBrand(
            @ApiParam(value = "接受要删除品牌的id") @PathVariable int id){

        ResultResponse<Void> result = null;
        try {
            int i = brandService.removeBrandById(id);
            if(i > 0){
                result = new ResultResponse<>(200,"删除品牌成功");
            }else{
                result = new ResultResponse<>(201,"删除品牌失败");
            }
        }catch (RuntimeException e){
            result = new ResultResponse<>(555,"系统错误");
        }
        return result;
    }



    @ApiOperation(value = "查询所有品牌")
    @GetMapping("/BrandAllList")
    public ResultResponse<List<Brand>> BrandAllList(){
        ResultResponse<List<Brand>> result = null;
        try {
            List<Brand> lists = brandService.findAll();
            result = new ResultResponse<>(200,"查询所有品牌成功",lists);
        }catch (RuntimeException e){
            result = new ResultResponse<>(201,"查询所有品牌失败");
        }
        return result;
    }

}
